package com.river.boot.biz.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.river.boot.biz.converter.ProductConverter;
import com.river.boot.biz.mapper.ProductMapper;
import com.river.boot.biz.model.entity.Product;
import com.river.boot.biz.model.form.ProductForm;
import com.river.boot.biz.model.form.ProductSpecConfigForm;
import com.river.boot.biz.model.form.ProductSpecForm;
import com.river.boot.biz.model.query.ProductQuery;
import com.river.boot.biz.model.vo.ProductVO;
import com.river.boot.biz.service.ProductService;
import com.river.boot.biz.service.ProductSpecService;
import com.river.boot.common.exception.BusinessException;
import com.river.boot.common.result.ResultCode;
import com.river.boot.security.utils.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 菜品服务实现类
 *
 * @author JiangCheng Xiang
 * @since 2025-04-24 21:04
 */
@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    private final ProductConverter productConverter;
    private final ProductSpecService productSpecService;

    /**
     * 获取菜品分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<ProductVO>} 菜品分页列表
     */
    @Override
    public IPage<ProductVO> getProductPage(ProductQuery queryParams) {
        Page<ProductVO> pageVO = this.baseMapper.getProductPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        List<ProductVO> records = pageVO.getRecords();
        if (CollUtil.isNotEmpty(records)) {
            records.forEach(productVO -> productVO.setTags(productVO.tags()));
        }
        return pageVO;
    }

    /**
     * 获取菜品表单数据
     *
     * @param id 菜品ID
     * @return
     */
    @Override
    public ProductForm getProductFormData(Long id) {
        Product entity = this.getById(id);
        ProductForm form = productConverter.toForm(entity);
        form.setImageList(entity.imageList());
        form.setTags(entity.tags());
        return form;
    }

    /**
     * 新增菜品
     *
     * @param formData 菜品表单对象
     * @return
     */
    @Override
    public boolean saveProduct(ProductForm formData) {
        Product entity = productConverter.toEntity(formData);
        entity.imageList(formData.getImageList());
        entity.tags(formData.getTags());
        entity.setCreateBy(SecurityUtils.getUserId());
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }

    /**
     * 更新菜品
     *
     * @param id       菜品ID
     * @param formData 菜品表单对象
     * @return
     */
    @Override
    public boolean updateProduct(Long id, ProductForm formData) {
        Product entity = productConverter.toEntity(formData);
        entity.imageList(formData.getImageList());
        entity.tags(formData.getTags());
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }

    /**
     * 删除菜品
     *
     * @param ids 菜品ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteProducts(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的菜品数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Transactional
    @Override
    public boolean saveProductSpec(ProductSpecConfigForm formData) {
        Product product = this.getById(formData.getId());
        if (Objects.isNull(product)) {
            throw new BusinessException(ResultCode.PRODUCT_EXIST);
        }

        productSpecService.batchSaveProductSpec(formData.getId(), formData.getSpecifications());
        return true;
    }

    @Override
    public ProductSpecConfigForm getProductSpec(Long id) {
        Product product = this.getById(id);
        if (Objects.isNull(product)) {
            throw new BusinessException(ResultCode.PRODUCT_EXIST);
        }

        ProductSpecConfigForm productSpecConfig = new ProductSpecConfigForm();
        List<ProductSpecForm> specifications = productSpecService.getProductSpecsByProductId(id);
        productSpecConfig.setId(product.getId());
        productSpecConfig.setSpecifications(specifications);
        return productSpecConfig;
    }

}
